{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "67e1073b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "6566dcc5",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.loadtxt('../data/USA_Housing.csv',delimiter=',',dtype=str)\n",
    "data = data[1:].astype(float)\n",
    "data = np.random.permutation(data)\n",
    "\n",
    "ratio = 0.8\n",
    "split = int(len(data)*ratio)\n",
    "train = data[:split]\n",
    "test = data[split:]\n",
    "\n",
    "scaler = StandardScaler()\n",
    "scaler.fit(train)\n",
    "train = scaler.transform(train)\n",
    "test = scaler.transform(test)\n",
    "\n",
    "x_train, x_test = train[:,:-1], test[:,:-1]\n",
    "y_train, y_test = train[:,-1], test[:, -1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "13efe9fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.97238059e-15],\n",
       "       [6.46967569e-01],\n",
       "       [4.67248400e-01],\n",
       "       [3.43829701e-01],\n",
       "       [3.73284593e-03],\n",
       "       [4.21293189e-01]])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = np.concatenate([np.ones(len(x_train)).reshape(-1,1),x_train],axis=1)\n",
    "y = y_train.reshape(-1,1)\n",
    "theta = np.linalg.inv(X.T @ X) @ X.T @ y\n",
    "theta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "c095280e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.2801110629475708"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test = np.concatenate([np.ones(len(x_test)).reshape(-1,1),x_test],axis=1)\n",
    "y_pred = X_test @ theta\n",
    "rmse_loss = np.sqrt(np.square(y_pred - y_test.reshape(-1,1)).mean())\n",
    "rmse_loss"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "younger",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.23"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
